#include<iostream>
#include<pthread.h>
#include<vector>
#include<unistd.h>
#define NUM 5
pthread_cond_t cond = PTHREAD_COND_INITIALIZER;
pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
int flag = 1000;
void *rountine(void* args)
{
    std::string name=static_cast<char*>(args);
    while(true)
    {
        
        pthread_mutex_lock(&mutex);
        pthread_cond_wait(&cond,&mutex);
        std::cout<<name<<" "<<flag<<std::endl;
        flag++;
        sleep(1);
        pthread_mutex_unlock(&mutex);
    }
}
int main()
{
    std::vector<pthread_t> threads; 

    for(int i=0;i<NUM;i++)
    {
        pthread_t tid;
        char name[64];
        snprintf(name,sizeof(name),"pthread-%d",i);
        pthread_create(&tid,nullptr,rountine,name);
        threads.push_back(tid);
        sleep(1);
    }
    while(true)
    {
        pthread_cond_signal(&cond);
        sleep(3);
    }
    for(auto&thread:threads)
    {
        int n = pthread_join(thread,nullptr);
        (void)n;
    }
    return 0;
}